﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp.Delegate
{
    using System.Net.Mime;

    public delegate void MyDelegate(string msg);




    public class InstanceMethodDelegate
    {

        public void InstanceShowMsg(string msg)
        {
            Console.WriteLine(string.Format("instance method invoke,msg:{0}",msg));

        }

    }

    public class StaticMethodDelegate
    {
        public static void StaticShowMsg(string msg)
        {
            Console.WriteLine(string.Format("static method invoke,msg:{0}", msg));
        }
    }


    public class BaseScenarioInvoker
    {


        public static void Invoke()
        {
            MyDelegate statDelegate = new MyDelegate(StaticMethodDelegate.StaticShowMsg);


            Console.WriteLine(string.Format("d Targert is:{0}",statDelegate.Target));
            Console.WriteLine(string.Format("d Method is:{0}", statDelegate.Method.Name));
            Console.WriteLine(string.Format("d InvocationList count  is:{0}", statDelegate.GetInvocationList().Count()));


            InstanceMethodDelegate instanceMethodDelegate = new InstanceMethodDelegate();


            var instanceDelegate = new MyDelegate(instanceMethodDelegate.InstanceShowMsg);

            var d = (MyDelegate)System.Delegate.Combine(statDelegate, instanceDelegate);
            Console.WriteLine("combine...");


            if (d.Equals(instanceDelegate))
            {
                Console.WriteLine("Equals");
            }

            Console.WriteLine(string.Format("d Targert is:{0}", d.Target));
            Console.WriteLine(string.Format("d Method is:{0}", d.Method.Name));
            Console.WriteLine(string.Format("d InvocationList count  is:{0}", d.GetInvocationList().Count()));
            Console.WriteLine(string.Format("d InvocationList 0  is:{0}", d.GetInvocationList()[0].Method));
            Console.WriteLine(string.Format("d InvocationList 1  is:{0}", d.GetInvocationList()[1].Method));
           
            
            
            
            Console.WriteLine("invoke...");
            var msg = "error";

            foreach (MyDelegate item in d.GetInvocationList())
            {
                item(msg);
            }

            d(msg);

        }

    }

    


}
